package com.xd.pre.modules.tpshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.pre.modules.tpshop.domain.TpCoupon;
import com.xd.pre.modules.tpshop.domain.TpCouponList;
import com.xd.pre.modules.tpshop.service.ITpCouponListService;
import com.xd.pre.modules.tpshop.service.ITpCouponService;
import com.xd.pre.modules.tpshop.virtuaBean.CouponVirtual;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.xd.pre.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/userCouponList")
public class TpCouponListController  {  //implements ITpCouponListAction

    @Autowired
    private ITpCouponListService cl_biz;

    @Autowired
    private ITpCouponService cou_biz;

    //【1】 http://localhost:8080/tpshop_server/userCouponList/findCouponList_byUid?uid=1	// 测试已通过
    @RequestMapping(value = "/findCouponList_byUid", method = RequestMethod.GET)

    public Result findCouponList_byUid(HttpServletRequest request) {

        System.out.println("测试--1-request.getParameter(uid)-->: " + request.getParameter("uid")); //
        String uid = request.getParameter("uid"); //

        Result result = new Result();
        if (null != uid && !"".equals(uid)) {
            List<TpCouponList> couponListId_list = getTpCouponListsByUserId(uid);
//            List<Integer> couponListId_list = cl_biz.findCouponList_byUid(Integer.parseInt(uid));
            if (couponListId_list != null && !couponListId_list.isEmpty()) {

//				for(int couponListId : couponListId_list){								//   couponListId
//					int couponId = cl_biz.findCouponId_byCouponlist_id(couponListId);	//通过 couponListId 得到对应的 couponId
//					TpCoupon coupon = cou_biz.findById(couponId);
//					Double couponPrice = coupon.getMoney();
//				}
                List<BigDecimal> couponPrice_list = new ArrayList<>();
                for (int i = 0; i < couponListId_list.size(); i++) {
                    TpCouponList byId = couponListId_list.get(i);
                    Integer couponId = byId.getCouponId();
//                    int couponId = cl_biz.findCouponId_byCouponlist_id(couponListId);	//通过 couponListId 得到对应的 couponId
                    TpCoupon coupon = cou_biz.getById(couponId);
//                    TpCoupon coupon = cou_biz.findById(couponId);
                    BigDecimal couponPrice = coupon.getMoney();
                    couponPrice_list.add(couponPrice);
                }
                System.out.println("优惠券价格list中 第一个位置的价格 1： " + couponPrice_list.size());    //3
                double couponPrice_sum = 0;
                couponPrice_sum = couponPrice_list.stream().mapToDouble(e -> e.doubleValue()).reduce(0, Double::sum);

                result.setData(couponPrice_sum);
            } else {
                result.setData("通过uid在db中没有查询到结果 ！");//
            }
        } else {
            result.setData("传入的uid为null ！");//
        }


        return result;
    }

    private List<TpCouponList> getTpCouponListsByUserId(String uid) {
        LambdaQueryWrapper<TpCouponList> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpCouponList::getUserId, uid);
        return cl_biz.list(lambdaQuery);
    }

    //此方法只用作测试, 目的是为【3】服务 最终被 TpCartAction中的findContent_inConformOrder()方法调用 ！！！
    //【2】 http://localhost:8080/tpshop_server/userCouponList/findCouponVirtualList_byUid?uid=1
    @RequestMapping(value = "/findCouponVirtualList_byUid", method = RequestMethod.GET)
    public Result findCouponVirtualList_byUid(HttpServletRequest request) {
        Result result = new Result();
        String uid = request.getParameter("uid"); //

        List<TpCouponList> couponList_list = getTpCouponListsByUserId(uid);
//        List<TpCouponList> couponList_list = cl_biz.findCouponVirtualList_part1_byUid(Integer.parseInt(uid));
        List<CouponVirtual> cv_list = new ArrayList<CouponVirtual>();
        CouponVirtual cv = null;
        if (couponList_list != null && !couponList_list.isEmpty()) {
            for (int i = 0; i < couponList_list.size(); i++) {
                TpCouponList item_ = couponList_list.get(i);
                cv = new CouponVirtual(
                        item_.getCouponlistId() + "",
                        item_.getType() + "",
                        item_.getUserId() + "",
                        item_.getOrderId() + "",
                        item_.getUseTime() + "",
                        item_.getCode() + "",
                        item_.getSendTime() + "",

                        //				( `tp_coupon`.`coupon_id`- 优惠券类型表ID)
                        getTpCouponById(item_).getUseEndTime() + "",
                        item_.getCouponId() + "",
                        getTpCouponById(item_).getName(),
                        getTpCouponById(item_).getMoney() + "",
                        getTpCouponById(item_).getCondition() + ""
                );
                cv_list.add(cv);
            }
            result.setData(cv_list);
        }
        return result;
    }

    private TpCoupon getTpCouponById(TpCouponList item_) {
        return cou_biz.getById(item_.getCouponId());
    }

    //【3】  移动到了 TpCouponListServiceImpl.java
//    @Override //TpCartAction中的findContent_inConformOrder()方法需要调用此方法
//    public List<CouponVirtual> findCouponVirtualList_byUid_invoked(int uId) {
//        List<TpCouponList> couponList_list = getTpCouponListsByUserId(uId+"");
////        List<TpCouponList> couponList_list = cl_biz.findCouponVirtualList_part1_byUid(uId);
//        List<CouponVirtual> cv_list = new ArrayList<CouponVirtual>();
//        CouponVirtual cv = null;
//        if (couponList_list != null && !couponList_list.isEmpty()) {
//            for (int i = 0; i < couponList_list.size(); i++) {
//                TpCouponList temp_ = couponList_list.get(i);
//                cv = new CouponVirtual(
//                        temp_.getCouponlistId() + "",
//                        temp_.getType() + "",
//                        temp_.getUserId() + "",
//                        temp_.getOrderId() + "",
//                        temp_.getUseTime() + "",
//                        temp_.getCode() + "",
//                        temp_.getSendTime() + "",
//
//                        //				( `tp_coupon`.`coupon_id`- 优惠券类型表ID)
//                        getTpCouponById(temp_).getUseEndTime() + "",
//                        temp_.getCouponId() + "",
//                        getTpCouponById(temp_).getName(),
//                        getTpCouponById(temp_).getMoney() + "",
//                        getTpCouponById(temp_).getCondition() + ""
//                );
//                cv_list.add(cv);
//            }
//
//        }
//        return cv_list;
//    }

}
